package com.mindspore.posewish;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class FileIO {
    private static final String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    private static final String fileName = "PostWish.pw";
    private static FileInputStream inputStream = null;
    private static FileOutputStream outputStream = null;
    private static BufferedOutputStream buffer = null;

    /* 存储文件 */
    static public void saveFile() {
        final String time = String.valueOf(System.currentTimeMillis()).substring(8, 12);

        /*数据来源*/
        List<Posenet.Person> allPost = Global.postData.readAll();
        Log.d("x坐标", allPost.get(0).toString());

        File file = new File(absolutePath, fileName);
        try {
            if (file.exists()) file.delete();
            file.createNewFile();

            outputStream = new FileOutputStream(file);
            buffer = new BufferedOutputStream(outputStream);
            for (Posenet.Person post : allPost) {
                buffer.write((post.toString() + '\n').getBytes());
            }
//            buffer.write((allPost.toString()).getBytes());
            buffer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (buffer != null) {
                try {
                    buffer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        Global.postData.clean();
    }

    /* 读取文件 */
    static public String readFile() {
        Global.parseData = new PostData();

        File file = new File(absolutePath, fileName);
        String str = null;
        try {
            inputStream = new FileInputStream(file);


            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((str = bufferReader.readLine()) != null) {
                Global.parseData.push(parseLine(str));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

    static public Posenet.Person parseLine(String line) {
        Posenet.Person per = new Posenet.Person();


        String[] tmp = line.split("/");
        per.score = Float.parseFloat(tmp[1]);

        Posenet.KeyPoint[] keyPoints = new Posenet().getKeyPoints();

        String tmp1 = tmp[0].substring(1, tmp[0].length() - 1);
        String[] items = tmp1.split(", "); // 分割成点的字串

        for (int i = 0; i < items.length; i++) {  // 遍历一个动作所有的点
            String item1 = items[i].substring(1, items[i].length() - 1); // 去除括号

            String[] tmp2 = item1.split(":");
            keyPoints[i].score = Float.parseFloat(tmp2[1]);

            String[] pos = tmp2[0].split(",");
            float x = Float.parseFloat(pos[0]);  // x
            float y = Float.parseFloat(pos[1]);  // y
            keyPoints[i].position.x = x;
            keyPoints[i].position.y = y;
        }
        per.keyPoints =  Arrays.asList(keyPoints);
        return per;
    }

}
